package com.smart.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.entity.Employee;
import com.smart.mapper.EmployeeMapper;
import com.smart.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Cookies
 * @since 2024-05-06
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Override
    public IPage getEmployeeList(Employee condition, Integer current, Integer size) {
        IPage page = new Page(current, size);
        IPage employeeData;
        if (condition == null) {
            employeeData = lambdaQuery()
                    .page(page);
        } else {
            employeeData = lambdaQuery()
                    .like(StrUtil.isNotBlank(condition.getUid()), Employee::getUid, condition.getUid())
                    .like(StrUtil.isNotBlank(condition.getName()), Employee::getName, condition.getName())
                    .eq(condition.getEntryDate() != null, Employee::getEntryDate, condition.getEntryDate())
                    .eq(StrUtil.isNotBlank(condition.getGender()), Employee::getGender, condition.getGender())
                    .eq(StrUtil.isNotBlank(condition.getDepartment()), Employee::getDepartment, condition.getDepartment())
                    .eq(StrUtil.isNotBlank(condition.getPost()), Employee::getPost, condition.getPost())
                    .eq(StrUtil.isNotBlank(condition.getSignIn()), Employee::getSignIn, condition.getSignIn())
                    .page(page);
        }
        return employeeData;
    }

    @Override
    public boolean addEmployee(Employee employee) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String joinDate = LocalDateTime.now().format(formatter);
        String[] dateArray = joinDate.split("-");
        String[] yearArray = dateArray[0].split("0");
        String datePart = yearArray[1] + dateArray[1] + dateArray[2];
        Integer count = lambdaQuery().likeRight(Employee::getUid, datePart)
                .count();
        count += 1;
        String countString = count < 10 ? "0" + count : count.toString();
        String uid = datePart + countString;
        employee.setUid(uid);
        return save(employee);
    }

    @Override
    public List<Map<String, Integer>> getAttendanceData() {
        List<Map<String, Integer>> attendanceDataList = new ArrayList<>();
        Map<String, Integer> todayData = new HashMap<>();
        todayData.put("normal", lambdaQuery().eq(Employee::getSignIn, "正常").count());
        todayData.put("late", lambdaQuery().eq(Employee::getSignIn, "迟到").count());
        todayData.put("leave", lambdaQuery().eq(Employee::getSignIn, "请假").count());
        todayData.put("signOut", lambdaQuery().eq(Employee::getSignOut, "正常").count());
        attendanceDataList.add(todayData);
        Map<String, Integer> yesterdayData = new HashMap<>();
        yesterdayData.put("normal", lambdaQuery().eq(Employee::getSignInYesterday, "正常").count());
        yesterdayData.put("late", lambdaQuery().eq(Employee::getSignInYesterday, "迟到").count());
        yesterdayData.put("leave", lambdaQuery().eq(Employee::getSignInYesterday, "请假").count());
        yesterdayData.put("signOut", lambdaQuery().eq(Employee::getSignOutYesterday, "正常").count());
        attendanceDataList.add(yesterdayData);
        return attendanceDataList;
    }
}
